package com.uchiha.nova.mainService.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.uchiha.nova.core.model.dto.PasswordModifyDTO;
import com.uchiha.nova.core.model.dto.TenantDTO;
import com.uchiha.nova.core.model.dto.UserEntityDTO;
import com.uchiha.nova.core.model.dto.WechaterUserRegisterDTO;
import com.uchiha.nova.core.model.entity.RoleEntity;
import com.uchiha.nova.core.model.entity.TenantUserEntity;
import com.uchiha.nova.core.model.entity.UserEntity;
import com.uchiha.nova.core.model.entity.WechaterEntity;
import com.uchiha.nova.core.model.vo.NavMenuVO;
import com.uchiha.nova.core.tool.api.R;
import com.uchiha.nova.core.tool.support.BaseController;
import com.uchiha.nova.core.tool.support.Condition;
import com.uchiha.nova.core.tool.support.NovaUser;
import com.uchiha.nova.core.util.AesEncryptUtil;
import com.uchiha.nova.mainService.service.*;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 周林静
 * @Date 2022-01-19 13:52
 * @Version 1.0
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController extends BaseController {
    @Resource
    UserService userService;
    @Resource
    INavMenuService navMenuService;
    @Resource
    IRoleService roleService;

    @Resource
    IWechaterService wechaterService;
    @Resource
    ITenantUserService tenantUserService;
    @Autowired
    PasswordEncoder passwordEncoder;




    @GetMapping("/info/avue")
    public R getUserInfo(Long id){
        return R.data(userService.getWebUserInfoVO(id));
    }

    @GetMapping("/getAll")
    public R getAll(){
        return R.data(userService.list());
    }
    @GetMapping("/navMenu/avue")
    public R navMenuAvue(Long id){
        NovaUser novaUser = getNovaUser();
        List<NavMenuVO> navMenuVOS;
//        if(novaUser.getRoleName().equals("ROLE_Tenant_Admin")){
//            navMenuVOS = (List<NavMenuVO>) redisTemplate.opsForValue().get(RedisCacheConstants.NOVA_TENANT_NAV_MENU +novaUser.getTenantId());
//        }else{
//            List<RoleEntity> roleEntities = roleService.getUserRoles(id);
//            navMenuVOS = navMenuService.getByRoles(roleEntities);
//        }
        List<RoleEntity> roleEntities = roleService.getUserRoles(id);
        navMenuVOS = navMenuService.getByRoles(roleEntities);
        return R.data(navMenuVOS);
    }


    @PostMapping("/tenant/init")
    public void tenantAdd(@RequestBody TenantDTO tenantDTO){
        tenantUserService.initTenant(tenantDTO);
    }



    @PostMapping("/getOne")
    public R<UserEntity> getOne(@RequestBody UserEntity userEntity){
        log.info("getOne:{}",userEntity);
        return R.data(userService.getOne(Condition.getQueryWrapper(userEntity)));
    }
    @GetMapping("/getAdminByUserName")
    public R getAdminByUserName(@RequestParam String username){
        log.info("getAdminByUserName:{}",username);
        UserEntity userEntity = new UserEntity();
        userEntity.setTenantId("1111");
        userEntity.setUsername(username);
        UserEntity one = userService.getOne(Condition.getQueryWrapper(userEntity));
        UserEntityDTO userEntityDTO = new UserEntityDTO();
        BeanUtil.copyProperties(one,userEntityDTO);
        return R.data(userEntityDTO);
    }
    @PostMapping("/passwordModify")
    public R passwordModify(@RequestBody PasswordModifyDTO dto){
        UserEntity entity = userService.getById(dto.getUserId());
        boolean matches = passwordEncoder.matches(dto.getPasswordOld(), entity.getPassword());
        if(matches){
            entity.setPassword(passwordEncoder.encode(dto.getPasswordNew()));
            userService.updateById(entity);
            return R.success("密码修改成功");
        }else{
            return R.fail("密码不正确");
        }

    }

    @GetMapping("/resetPassword")
    public R resetPassword(Long id){
        UserEntity entity = userService.getById(id);
        String p = passwordEncoder.encode("123456");
        entity.setPassword(p);
        userService.updateById(entity);
        return R.success("密码修改成功");
    }

    @GetMapping("/passwordEncode")
    public R passwordEncode(@RequestParam String password){
        return R.data(passwordEncoder.encode(password));
    }




    @PostMapping("/wechat/register")
    public R wechatRegister(@RequestBody WechaterUserRegisterDTO dto){
        String username = dto.getUsername();
        String password = dto.getPassword();
        UserEntity userEntity = userService.getOne(new QueryWrapper<UserEntity>().eq("username",username));
        if(ObjectUtil.isEmpty(userEntity)){
            return R.fail("无效的用户名");
        }
        boolean match = passwordEncoder.matches(password,userEntity.getPassword());
        if(!match){
            return R.fail("无效的密码");
        }
        WechaterEntity wechaterEntity = new WechaterEntity();
        wechaterEntity.setMobile(dto.getUsername());
        wechaterEntity.setOpenId(dto.getOpenid());
        wechaterEntity.setUserId(userEntity.getId());
        wechaterEntity.setTenant(userEntity.getTenantId());
        wechaterService.save(wechaterEntity);
        return R.success("注册成功");
    }

    @GetMapping("/wechat/info")
    public R getWeChaterInfo(@RequestParam Long userId){
        UserEntity userEntity = userService.getById(userId);
        Map<String,Object> map = new HashMap<>();
        map.put("username",userEntity.getUsername());
        map.put("userId",userId);
        map.put("name",userEntity.getName());
        map.put("avatar",userEntity.getAvatar());
        map.put("mobile",userEntity.getMobile());
        return R.data(map);
    }
}
